package com.hnas.eCard.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.hnas.eCard.dao.EntityCardOrderDetailDao;
import com.hnas.eCard.dao.EntityDenominationReDao;
import com.hnas.eCard.dao.SaleCardOrderDetailDao;
import com.hnas.eCard.orm.EntityCardOrderDetail;
import com.hnas.eCard.orm.EntityDenominationRe;
import com.hnas.eCard.orm.SaleCardOrderDetail;
import com.hnas.eCard.service.SaleCardOrderService;
import com.hnas.eCard.service.dto.DTOEntityCardOrder;
import com.hnas.eCard.service.dto.DTOEntityCardOrderDetail;
import com.hnas.eCard.service.dto.DTOSaleCardOrder;
import com.hnas.eCard.service.dto.DTOSaleCardOrderDetail;
import com.hnas.trade.dao.OrderDao;
import com.hnas.trade.orm.Order;

@Component("saleCardOrderService")
@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
public class SaleCardOrderServiceImpl implements SaleCardOrderService{
	
	@Autowired
	private OrderDao orderDao;
	@Autowired
	private SaleCardOrderDetailDao saleCardOrderDetailDao;
	@Autowired
	private EntityCardOrderDetailDao entityCardOrderDetailDao;
	@Autowired
	private EntityDenominationReDao entityDenominationReDao;
	
	@Override
	public Boolean save(DTOSaleCardOrder orderDto,DTOSaleCardOrderDetail detailDto) {
		//保存订单信息
		Order order = this.orderDao.saveOrUpdate(orderDto);
		//保存订单详情信息
		SaleCardOrderDetail detail = this.saleCardOrderDetailDao.saveOrUpdate(detailDto);
		return (order != null && detail != null);
	}
	@Override
	public DTOSaleCardOrder findByOrderNo(String orderNo) {
		return this.saleCardOrderDetailDao.findSaleCardOrderByOrderNo(orderNo);
	}
	@Override
	public DTOSaleCardOrder getBySwiftNumber(String swiftNumber) {
		return this.saleCardOrderDetailDao.findSaleCardOrderBySwiftNumber(swiftNumber);
	}
	@Override
	public boolean saveEntityCard(DTOSaleCardOrder orderDto,DTOEntityCardOrderDetail entityCardOrderDetail,List<EntityDenominationRe> entityDenominationReList) {
		boolean result = true;
		Order order = this.orderDao.saveOrUpdate(orderDto);
		if(order == null) result = false;
		//保存订单详情信息
		EntityCardOrderDetail detail = this.entityCardOrderDetailDao.saveOrUpdate(entityCardOrderDetail);
		if(detail == null) result = false;
		//保存订单-面额-数量详情
		for(EntityDenominationRe re : entityDenominationReList) {
			EntityDenominationRe temp = this.entityDenominationReDao.saveOrUpdate(re);
			if(temp == null) result = false;
		}
		return result;
	}
	@Override
	public DTOEntityCardOrder getEntityBySwiftNumber(String swiftNumber) {
		return this.entityCardOrderDetailDao.findEntityCardOrderBySwiftNumber(swiftNumber);
	}
	@Override
	public List<DTOEntityCardOrder> findEntityOrderListByAccId(String accId) {
		return this.entityCardOrderDetailDao.findEntityOrderListByAccId(accId);
	}
	@Override
	public DTOEntityCardOrder getEntityByOrderNo(String orderNo) {
		return this.entityCardOrderDetailDao.getEntityByOrderNo(orderNo);
	}
	@Override
	public DTOEntityCardOrder getCommoditByOrderNo(String orderNo) {
		return entityCardOrderDetailDao.getCommoditByOrderNo(orderNo);
	}



}
